package com.woniuxy.yoga.product.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.product.dao.model.TeamProduct;
import com.woniuxy.yoga.product.model.dto.AllTeamProductDTO;
import com.woniuxy.yoga.product.model.dto.TeamProductByIdDTO;
import com.woniuxy.yoga.product.model.dto.TeamProductDTO;
import com.woniuxy.yoga.product.model.form.AddTeamProductForm;
import com.woniuxy.yoga.product.model.form.QueryTeamProductForm;
import com.woniuxy.yoga.product.model.param.AddTeamProductParam;
import com.woniuxy.yoga.product.model.param.QueryTeamProductParam;
import com.woniuxy.yoga.product.service.service.TeamProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 团课产品表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/teamProduct")
@Api(tags = "团课产品服务相关业务接口")
@Validated
public class TeamProductController {

    @Autowired
    private TeamProductService teamProductService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 用户查询团课产品
     * @param storeId
     * @return
     */
    @GetMapping("/queryNormalTeam.us")
    @ApiOperation("用户查询待开课团课产品接口")
    public Result<List<TeamProductDTO>> queryNormalTeam(){
        List<TeamProductDTO> teamProductDTOS = teamProductService.queryNormalTeam();
        return Result.getListResult(TeamProductDTO.class).setMessage("用户查询待开课团课产品信息成功").setData(teamProductDTOS);
    }

    /**
     * 教练ID查询团队产品
     * @param ids
     * @return
     */
    @PostMapping("/getByCoachId")
    @ApiOperation("教练ID查询团队产品接口")
    public Result<List<AllTeamProductDTO>> getByCoachId(@RequestBody List<Integer> ids){
        //获取数据
        List<AllTeamProductDTO> allTeamProductDTOS = teamProductService.getByCoachId(ids);
        //返回数据
        return Result.getListResult(AllTeamProductDTO.class).setMessage("根据教练ID查询团队产品信息成功").setData(allTeamProductDTOS);
    }

    /**
     * 新增团队产品(设置定时任务 定时开课)
     * @param addTeamProductForm
     * @param authenToken
     * @return
     */
    @PostMapping("/addTeamProduct.bu")
    @ApiOperation("新增团队产品接口")
    @YogaLog("新增团队产品")
    public Result<?> addTeamProduct(@Valid @RequestBody AddTeamProductForm addTeamProductForm,@RequestHeader("authen_token") String authenToken) throws Exception {
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析token 获取商家ID
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        //对象转换
        AddTeamProductParam param = BeanUtil.toBean(addTeamProductForm, AddTeamProductParam.class);
        param.setAccountId(businessId);
        //调用方法
        teamProductService.addTeamProduct(param);
        return Result.getResult().setMessage("新增团队产品成功");
    }

    /**
     * 查询团队产品
     * @param teamProductId
     * @return
     */
    @GetMapping("/getById/{teamProductId}")
    @ApiOperation("团队产品ID查询团队产品接口")
    @ApiImplicitParam(name = "teamProductId",value = "团队产品ID")
    public Result<TeamProductByIdDTO> getById(@Valid @NotNull(message = "团队产品ID不能为空") @PathVariable("teamProductId") Integer teamProductId){
        //获取数据
        TeamProduct teamProduct = teamProductService.getById(teamProductId);
        //对象转换
        TeamProductByIdDTO teamProductByIdDTO = BeanUtil.toBean(teamProduct, TeamProductByIdDTO.class);
        //返回数据
        return Result.getResult(TeamProductByIdDTO.class).setMessage("根据团队产品ID查询团队产品成功").setData(teamProductByIdDTO);
    }

    /**
     * 团队产品取消
     * @param teamProductId
     * @return
     */
    @GetMapping("/cancelTeamProduct/{teamProductId}.bu")
    @ApiOperation("团队产品取消接口")
    @ApiImplicitParam(name = "teamProductId",value = "团队产品ID")
    public Result<?> cancelTeamProduct(@Valid @NotNull(message = "团队产品ID不能为空") @PathVariable("teamProductId") Integer teamProductId){
        teamProductService.cancelTeamProduct(teamProductId);
        return Result.getResult().setMessage("团队产品取消成功");
    }

    /**
     * 团队产品完成
     * @param teamProductId
     * @return
     */
    @GetMapping("/completeTeamProduct/{teamProductId}")
    @ApiOperation("团队产品完成接口")
    @ApiImplicitParam(name = "teamProductId",value = "团队产品ID")
    public Result<?> completeTeamProduct(@Valid @NotNull(message = "团队产品ID不能为空") @PathVariable("teamProductId") Integer teamProductId){
        teamProductService.completeTeamProduct(teamProductId);
        return Result.getResult().setMessage("团队产品完成成功");
    }

    /**
     * 商家查询自己的所有团队产品
     * @param queryTeamProductForm
     * @return
     */
    @PostMapping("/queryTeamProduct.bu")
    @ApiOperation("商家查询自己的所有团队产品接口")
    public Result<PageInfo> queryTeamProduct(@Valid @RequestBody QueryTeamProductForm queryTeamProductForm){
        PageInfo pageInfo = teamProductService.queryTeamProduct(BeanUtil.toBean(queryTeamProductForm, QueryTeamProductParam.class));
        return Result.getResult(PageInfo.class).setMessage("商家查询自己的所有团队产品成功").setData(pageInfo);
    }

    /**
     * 团课预约人数加一
     * @param teamProductId
     * @return
     */
    @GetMapping("/modifyBookNum/{teamProductId}")
    @ApiOperation("预约人数加一接口")
    @ApiImplicitParam(name = "teamProductId",value = "团队产品ID")
    public Result<?> modifyBookNum(@Valid @NotNull(message = "团队产品ID不能为空") @PathVariable("teamProductId") Integer teamProductId) throws Exception{
        teamProductService.modifyBookNum(teamProductId);
        return Result.getResult().setMessage("团队产品预约人数加一成功");
    }

    /**
     * 团队产品总天数减一
     * @param teamProductId
     * @return
     */
    @GetMapping("/hourCountMinusOne/{teamProductId}")
    @ApiOperation("团队产品总天数减一接口")
    @ApiImplicitParam(name = "teamProductId",value = "团队产品ID")
    public Result<Integer> hourCountMinusOne(@Valid @NotNull(message = "团队产品ID不能为空") @PathVariable("teamProductId") Integer teamProductId){
        Integer num = teamProductService.hourCountMinusOne(teamProductId);
        return Result.getResult(Integer.class).setMessage("团队产品总天数减一成功").setData(num);
    }
}

